package com.lfs.notify.service.zfb.service.impl;

import com.lfs.base.enums.AlipayTradeStatus;
import com.lfs.interfaces.dao.OrderChargeDao;
import com.lfs.interfaces.model.dto.OrderCharge;
import com.lfs.interfaces.model.dto.OrderDto;
import com.lfs.interfaces.model.dto.OrderNotifyDto;
import com.lfs.interfaces.model.zfb.ZFBNotifyParams;
import com.lfs.interfaces.order.service.OrderBaseService;
import com.lfs.interfaces.product.service.ProductService;
import com.lfs.notify.service.zfb.service.ZFBNotifyService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ZFBNotifyServiceImpl implements ZFBNotifyService {

    private static final Logger logger = LoggerFactory.getLogger(ZFBNotifyServiceImpl.class);

    @Resource
    private ProductService productService;

    @Resource
    private OrderChargeDao orderChargeDao;

    @Resource
    private OrderBaseService orderBaseService;

    @Override
    public String notifyZFB(ZFBNotifyParams zfbNotifyParams,List<OrderCharge> orderChargeList) {
        String result = "success";
        try {

            if (CollectionUtils.isNotEmpty(orderChargeList)) {
                OrderCharge orderInfo = orderChargeList.get(0);
                OrderNotifyDto orderNotify = new OrderNotifyDto();
                orderNotify.setAgtPhone(orderInfo.getAgtPhone());
                orderNotify.setReqStreamId(orderInfo.getDownOrderNo());
                orderNotify.setOrderNo(zfbNotifyParams.getOutTradeNo());
                orderNotify.setPrice(orderInfo.getPrice());

                orderInfo.setOutMoney(orderInfo.getOutMoney());
                orderInfo.setProfit(orderInfo.getProfit());
                OrderDto orderDto = new OrderDto();
                orderDto.setId(orderInfo.getId());
                orderDto.setOrderNo(orderInfo.getOrderNo());
                orderDto.setOutMoney(orderInfo.getOutMoney());
                orderDto.setProfit(orderInfo.getProfit());
                orderDto.setAgtNo(orderInfo.getAgtNo());
                orderDto.setChargeAddr(zfbNotifyParams.getSellerEmail());
                orderDto.setChargeMoney(orderInfo.getChargeMoney());
                orderDto.setUpOrderNo(zfbNotifyParams.getTradeNo() == null ? "" : zfbNotifyParams.getTradeNo());
                orderDto.setExp2(zfbNotifyParams.getBuyerId()+"|"+zfbNotifyParams.getBuyerLogonId());
                String trade_status = zfbNotifyParams.getTradeStatus();
                // 支付成功
                if (trade_status.equals(AlipayTradeStatus.TRADE_SUCCESS.getStatus())
                        || trade_status.equals(AlipayTradeStatus.TRADE_FINISHED.getStatus())) {
                    // 成功
                    orderDto.setState(0);
                    orderDto.setRemark("充值成功|回调");
                    orderBaseService.updateOrderAfterNotify(orderDto);
                    // 回写回调表
                    if (orderNotify.getReqStreamId() != null && orderNotify.getReqStreamId() != "") {
                        orderNotify.setState(0);
                        orderBaseService.sendNotify(orderNotify);
                    }
                } else {
                    // 失败，修改订单状态
                    orderDto.setState(1);
                    orderDto.setRemark("充值失败|回调");
                    orderBaseService.updateOrderAfterNotify(orderDto);
                    // 回写回调表
                    if (orderNotify.getReqStreamId() != null && orderNotify.getReqStreamId() != "") {
                        orderNotify.setState(1);
                        orderBaseService.sendNotify(orderNotify);
                    }
                    logger.error("支付宝回调未成功，支付宝交易状态：{},params:{}",trade_status,zfbNotifyParams.toString());
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            result = "Exception";
        }
        return result;
    }
}
